package com.server.xxl_job.jobHandler;

import com.server.liad.api.system.remote.RemoteSystemService;
import com.server.common.code.utils.Result;
import com.server.model.all.enums.SystemEnum;
import com.server.common.code.utils.ResultUtil;
import com.server.common.redis.service.RedisBlackWhiteListService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;


@Component
@Slf4j
public class RedisJobHandler {

    @Resource
    private RedisBlackWhiteListService redisBlackWhiteListService;

    @Resource
    private RemoteSystemService remoteSystemService;

    /**
     * 更新redis黑白名IP地址名单
     */
    @XxlJob("updateRedisBlackIpAddrList")
    public ReturnT<String> updateRedisBlackIpAddrList(){
        log.info("====更新redis黑白名IP地址名单===开始===");
        //更新黑名单IP地址
        Result<List<String>> blackIpAddrListResult = remoteSystemService.getBlackWhiteIpAddr(SystemEnum.blackWhiteListStateEnum.BACK.getCode());
        List<String> blackIpAddrList = ResultUtil.getDataThrowException(blackIpAddrListResult);
        redisBlackWhiteListService.addIpAddr(SystemEnum.blackWhiteListStateEnum.BACK,blackIpAddrList);


        //更新白名单IP地址
        Result<List<String>> whiteIpAddrListResult = remoteSystemService.getBlackWhiteIpAddr(SystemEnum.blackWhiteListStateEnum.WHITE.getCode());
        List<String> whiteIpAddrList = ResultUtil.getDataThrowException(whiteIpAddrListResult);
        redisBlackWhiteListService.addIpAddr(SystemEnum.blackWhiteListStateEnum.WHITE,whiteIpAddrList);

        log.info("====更新redis黑白名IP地址名单===完成===");
        return ReturnT.SUCCESS;
    }

}
